import * as Api from '@/syApis/syBb/apis.js'
	export default {
		
		watch: {
			//统计数量
			orderList(oldval, newval) {
				var inPurSaleQty = 0;
				var inPurSaleAmount = 0
				var outPurQty = 0;
				var outPurAmount = 0;
				var inGetQty = 0;
				var ouGetQty = 0;
				var cyQty = 0;
				var cyAmount = 0;
				var _this = this;
				this.orderList.forEach(item => {
					inPurSaleQty += item.real_qty;
					inPurSaleAmount += item.real_Amount;
					outPurQty += item.return_qty;
					outPurAmount += item.return_Amount;
					inGetQty += item.real_qty2;
					ouGetQty += item.return_qty2;
					cyQty += item.Qty;
					cyAmount += item.Amount;
				});
				//统计金额
				this.inPurSaleQty = inPurSaleQty;
				this.inPurSaleAmount = inPurSaleAmount;
				this.outPurQty = outPurQty;
				this.outPurAmount = outPurAmount;
				this.inGetQty = inGetQty;
				this.ouGetQty = ouGetQty;
				this.cyQty = cyQty;
				this.cyAmount = cyAmount;
			}
		},
		filters:{
			sendFloat(val=""){
			   if(val==""){
				   return 0;
			   }
			   return Math.round(parseFloat(val)*100)/100;
			}
		},
		onShow() {
			var $this = this;
			uni.getSystemInfo({
				success(res) {
					$this.wrapperHeight = res.windowHeight;
					$this.containerHeight = res.windowHeight - 50;
					$this.orderListHeight = res.windowHeight - 50;
					$this.orderLiHeight = res.windowHeight - 50 - 80;
				}
			});
			this.$eventHub.$off('fire');
		},
		onLoad(options) {
			console.log(options.type);
			//采购订单
			if (options.type == 0) {
				uni.setNavigationBarTitle({
					title: "业务员采购统计"
				})
				this.isSale = false;
			}
			//销售订单
			else {
				this.isSale = true;
				// this.OrderTitle = "业务员销售统计";
				uni.setNavigationBarTitle({
					title: "业务员销售统计"
				})
			}
			// 获取表格框的宽度
			let tbWidList = uni.getStorageSync("ACCORWidList", "");
			if (tbWidList != "") {
				this.tdWidthList = JSON.parse(tbWidList);
				this.tdWidth = JSON.parse(tbWidList);
			}
			let date = new Date();
			this.getTime();
			this.getCgOrder();
		},
		computed: {
			tdAllWidth() {
				var tdAllWidth = 0;
				this.tdWidth.forEach(item => {
					tdAllWidth += parseFloat(item);
				});
				tdAllWidth += this.tdWidth.length - 1;
				return tdAllWidth;
			}
		},
		// 
		methods: {
			getTime() {
				let yyyy = new Date().getFullYear();
				let mm = (new Date().getMonth() + 1)< 10 ? '0' + (new Date().getMonth() + 1) : (new Date().getMonth() + 1);
				let dd = new Date().getDate() < 10 ? '0' + new Date().getDate() : new Date().getDate();
				this.startTime = yyyy + '-' + mm + '-' + dd;
				this.endTime = yyyy + '-' + mm + '-' + dd;
			},
			changeListAl: function() {
				this.isList = !this.isList;
			},
			paixu1(type) {
				// this.paixu = !this.paixu;
				var len = this.orderList.length;
				var arr = this.orderList
				switch (type) {
					case 0:
						if (this.paixu == false) {
							this.paixu = true
							for (var i = 0; i < len; i++) {
								for (var j = 0; j < len - 1 - i; j++) {
									if (arr[j].real_qty > arr[j + 1].real_qty) { //相邻元素两两对比
										var temp = arr[j + 1];
										//元素交换
										arr[j + 1] = arr[j];
										arr[j] = temp;
									}
								}
							}
						} else {
							this.paixu = false
							for (var i = 0; i < len; i++) {
								for (var j = 0; j < len - 1 - i; j++) {
									if (arr[j].real_qty < arr[j + 1].real_qty) { //相邻元素两两对比
										var temp = arr[j];
										//元素交换
										arr[j] = arr[j + 1];
										arr[j + 1] = temp;
									}
								}
							}
						}
						return arr;
						break;

					case 1:
						if (this.paixu == false) {
							this.paixu = true
							for (var i = 0; i < len; i++) {
								for (var j = 0; j < len - 1 - i; j++) {
									if (arr[j].real_Amount > arr[j + 1].real_Amount) { //相邻元素两两对比
										var temp = arr[j + 1];
										//元素交换
										arr[j + 1] = arr[j];
										arr[j] = temp;
									}
								}
							}
						} else {
							this.paixu = false
							for (var i = 0; i < len; i++) {
								for (var j = 0; j < len - 1 - i; j++) {
									if (arr[j].real_Amount < arr[j + 1].real_Amount) { //相邻元素两两对比
										var temp = arr[j];
										//元素交换
										arr[j] = arr[j + 1];
										arr[j + 1] = temp;
									}
								}
							}
						}
						return arr;
						break;

					case 2:
						if (this.paixu == false) {
							this.paixu = true
							for (var i = 0; i < len; i++) {
								for (var j = 0; j < len - 1 - i; j++) {
									if (arr[j].return_qty > arr[j + 1].return_qty) { //相邻元素两两对比
										var temp = arr[j + 1];
										//元素交换
										arr[j + 1] = arr[j];
										arr[j] = temp;
									}
								}
							}
						} else {
							this.paixu = false
							for (var i = 0; i < len; i++) {
								for (var j = 0; j < len - 1 - i; j++) {
									if (arr[j].return_qty < arr[j + 1].return_qty) { //相邻元素两两对比
										var temp = arr[j];
										//元素交换
										arr[j] = arr[j + 1];
										arr[j + 1] = temp;
									}
								}
							}
						}
						return arr;
						break;

					case 3:
						if (this.paixu == false) {
							this.paixu = true
							for (var i = 0; i < len; i++) {
								for (var j = 0; j < len - 1 - i; j++) {
									if (arr[j].return_Amount > arr[j + 1].return_Amount) { //相邻元素两两对比
										var temp = arr[j + 1];
										//元素交换
										arr[j + 1] = arr[j];
										arr[j] = temp;
									}
								}
							}
						} else {
							this.paixu = false
							for (var i = 0; i < len; i++) {
								for (var j = 0; j < len - 1 - i; j++) {
									if (arr[j].return_Amount < arr[j + 1].return_Amount) { //相邻元素两两对比
										var temp = arr[j];
										//元素交换
										arr[j] = arr[j + 1];
										arr[j + 1] = temp;
									}
								}
							}
						}
						return arr;
						break;

					case 4:
						if (this.paixu == false) {
							this.paixu = true
							for (var i = 0; i < len; i++) {
								for (var j = 0; j < len - 1 - i; j++) {
									if (arr[j].Qty > arr[j + 1].Qty) { //相邻元素两两对比
										var temp = arr[j + 1];
										//元素交换
										arr[j + 1] = arr[j];
										arr[j] = temp;
									}
								}
							}
						} else {
							this.paixu = false
							for (var i = 0; i < len; i++) {
								for (var j = 0; j < len - 1 - i; j++) {
									if (arr[j].Qty < arr[j + 1].Qty) { //相邻元素两两对比
										var temp = arr[j];
										//元素交换
										arr[j] = arr[j + 1];
										arr[j + 1] = temp;
									}
								}
							}
						}
						return arr;
						break;

					case 5:
						if (this.paixu == false) {
							this.paixu = true
							for (var i = 0; i < len; i++) {
								for (var j = 0; j < len - 1 - i; j++) {
									if (arr[j].Amount > arr[j + 1].Amount) { //相邻元素两两对比
										var temp = arr[j + 1];
										//元素交换
										arr[j + 1] = arr[j];
										arr[j] = temp;
									}
								}
							}
						} else {
							this.paixu = false
							for (var i = 0; i < len; i++) {
								for (var j = 0; j < len - 1 - i; j++) {
									if (arr[j].Amount < arr[j + 1].Amount) { //相邻元素两两对比
										var temp = arr[j];
										//元素交换
										arr[j] = arr[j + 1];
										arr[j + 1] = temp;
									}
								}
							}
						}
						return arr;
						break;

					case 6:
						if (this.paixu == false) {
							this.paixu = true
							for (var i = 0; i < len; i++) {
								for (var j = 0; j < len - 1 - i; j++) {
									if (arr[j].real_qty2 > arr[j + 1].real_qty2) { //相邻元素两两对比
										var temp = arr[j + 1];
										//元素交换
										arr[j + 1] = arr[j];
										arr[j] = temp;
									}
								}
							}
						} else {
							this.paixu = false
							for (var i = 0; i < len; i++) {
								for (var j = 0; j < len - 1 - i; j++) {
									if (arr[j].real_qty2 < arr[j + 1].real_qty2) { //相邻元素两两对比
										var temp = arr[j];
										//元素交换
										arr[j] = arr[j + 1];
										arr[j + 1] = temp;
									}
								}
							}
						}
						return arr;
						break;

					case 7:
						if (this.paixu == false) {
							this.paixu = true
							for (var i = 0; i < len; i++) {
								for (var j = 0; j < len - 1 - i; j++) {
									if (arr[j].return_qty2 > arr[j + 1].return_qty2) { //相邻元素两两对比
										var temp = arr[j + 1];
										//元素交换
										arr[j + 1] = arr[j];
										arr[j] = temp;
									}
								}
							}
						} else {
							this.paixu = false
							for (var i = 0; i < len; i++) {
								for (var j = 0; j < len - 1 - i; j++) {
									if (arr[j].return_qty2 < arr[j + 1].return_qty2) { //相邻元素两两对比
										var temp = arr[j];
										//元素交换
										arr[j] = arr[j + 1];
										arr[j + 1] = temp;
									}
								}
							}
						}

						return arr;
						break;


				}

			},
			//设置监听
			AddListen() {
				var _this = this;
				this.$eventHub.$on('fire', function(data) {
					console.log(data);
					_this.startTime = data.StartDate;
					_this.EndDate = data.EndDate;
					_this.OperatorName = data.OperatorName == "" ? "全部业务员" : data.OperatorName;
					_this.getCgOrder(data);
					_this.$eventHub.$off('fire');
				});
			},
			//返回上一页
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
			//跳转到筛选条件页
			// filter() {
			// 	this.AddListen();
			// 	uni.navigateTo({
			// 		url: './accountOrderFilter'
			// 	})
			// },
			// #ifndef MP
			onNavigationBarButtonTap(e) {
				const index = e.index;
				if (index === 0) {
					this.AddListen();
					uni.navigateTo({
						url: './accountOrderFilter'
					})
				}else{
					this.changeListAl();
				}
			},
			// #endif
			//获取采购订单
			getCgOrder: function(filter) {
				let data = {};
				if (filter == undefined) {
					data = {
						StartDate: this.startTime,
						EndDate: this.endTime
					}
				} else {
					data = filter;
				}
				var _this = this;
				console.log(this.isSale);
				if (this.isSale == false) {
					Api.GetEmpPurchase(data, function(res) {
						if (res.state) {
							if (res.data.ErrorMsg == "获取登录信息失败") {

							} else if (res.data.ErrorMsg != "") {
								Api.showTip(res.data.ErrorMsg);
							} else {
								if(res.data.ResultMsg.length==0){
									Api.showTip("查无数据");
								}
								_this.orderList = _this.setGetTwoFloat(res.data.ResultMsg);
							}
						} else {
							Api.showTip("服务器开小差了");
						}
					});
				} else {
					console.log("销售订单");
					Api.GetEmpSale(data, function(res) {
						if (res.state) {
							if (res.data.ErrorMsg == "获取登录信息失败") {

							} else if (res.data.ErrorMsg != "") {
								Api.showTip(res.data.ErrorMsg);
							} else {
								_this.orderList = _this.setGetTwoFloat(res.data.ResultMsg);
							}
						} else {
							Api.showTip("服务器开小差了");
						}
					});
				}
			},
			//将数额保留两位小数
			setGetTwoFloat(arr) {
				arr.forEach((item, index) => {
					arr[index].real_qty = Api.setTwoFloat(item.real_qty);
					arr[index].real_Amount = Api.setTwoFloat(item.real_Amount);
					arr[index].return_qty = Api.setTwoFloat(item.return_qty);
					arr[index].real_qty2 = Api.setTwoFloat(item.real_qty2);
					arr[index].return_Amount = Api.setTwoFloat(item.return_Amount);
					arr[index].return_qty2 = Api.setTwoFloat(item.return_qty2);
					arr[index].Qty = Api.setTwoFloat(item.Qty);
					arr[index].Amount = Api.setTwoFloat(item.Amount);
				});
				console.log(arr);
				return arr;
			},
			lookMore: function(index) {
				this.nowRote = this.nowRote == index ? "-1" : index;
			},
			changeWidth() {
				this.showChange = true;
			},
			hideChaneg() {
				this.showChange = false;
				let tempList = [];
				this.tdWidth.forEach((item,index)=>{
					tempList.push(item);
				})
				this.tdWidthList = tempList;
				// uni.setStorageSync("tbWidList",JSON.stringify(tempList));
				// this.tdWidthList = this.tdWidth;
			},
			sureSetting() {
				let tempList = [];
				this.tdWidthList.forEach((item,index)=>{
					tempList.push(item);
				})
				this.tdWidth = tempList;
				uni.setStorageSync("ACCORWidList",JSON.stringify(tempList));
				this.showChange = false;
			},
		},
		data() {
			return {
				paixu: false,
				showChange: false,
				orderList: [],
				containerHeight: 100,
				wrapperHeight: 600,
				orderListHeight: 100,
				orderLiHeight: 100,
				isList: false,
				startTime: "",
				endTime: "",
				OperatorName: "全部业务员",
				//订单页标题
				OrderTitle: "",
				//是否销售订单
				isSale: false,
				nowRote: -1,
				//进货/销售数量总计
				inPurSaleQty: 0,
				//进货/销售金额总计
				inPurSaleAmount: 0,
				//退货数量总计
				outPurQty: 0,
				//退货金额总计
				outPurAmount: 0,
				//进货数总计
				inGetQty: 0,
				//退货数总计
				ouGetQty: 0,
				//差异数
				cyQty: 0,
				//差异金额
				cyAmount: 0,
				xjList: [{
						name: "小计"
					}, {
						name: "备注"
					}, {
						name: "备注"
					}, {
						name: ""
					}, {
						name: ""
					}, {
						name: ""
					}, {
						name: ""
					}, {
						name: ""
					},
					{
						name: ""
					},
					{
						name: ""
					},
					{
						name: ""
					}
				],
				list: [{
						name: 0,
						"openAndClose": "",
						"lookMoreClass": ""
					},
					{
						name: 0,
						"openAndClose": "",
						"lookMoreClass": ""
					}, {
						name: 0,
						"openAndClose": "",
						"lookMoreClass": ""
					}
				],
				//td宽度列表
				tdWidth: [50, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125],
				tdWidthList: [50, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125],
				tdHeaderList: [{
						name: "序号"
					},
					{
						name: "编号"
					}, {
						name: "业务员"
					}, {
						name: "销售数量"
					}, {
						name: "销售金额"
					}, {
						name: "退货数量"
					}, {
						name: "退货金额"
					}, {
						name: "差异数量"
					},
					{
						name: "差异金额"
					},
					{
						name: "销售数（未审）"
					},
					{
						name: "退货数（未审）"
					}
				]
			}
		}
	}